import { createApp } from 'vue'
import pinia from '/@/stores/index'
import { useUserInfo } from '/@/stores/user-info'
import CSUI from 'csui'
import { globalToken, startMicroApp, useMicroAppStore } from '@csui/micro-app'
import mock from '../mock'
import initRouter from './router'
import { directive } from '/@/directive'

// 引入csui样式
import '/@/theme/csui-theme.scss'
// 引入项目主题样式
import '/@/theme/index.scss'
// 引入font-icon
import '/@/assets/font-icon/font-awesome.min.css'
import '/@/assets/font-icon/font_ba.css'
// 最后引入全局通用组件
import globalComponents from './components'
import App from './App.vue'
import { getBaseApiUrl } from './hooks/request'

// microApp 启动microApp
startMicroApp()

// microApp 配置微应用映射
const microAppStore = useMicroAppStore(pinia)
microAppStore.setMicroAppMap({
  micro_App: 'http://localhost:8081',
  hrwauth: 'http://localhost:8200',
  hrwkpi: 'http://localhost:8100',
  hrwleave: 'http://localhost:8000'
})
// mockjs虚拟接口, 如后台接口已开发, 请删掉mock引用
mock(getBaseApiUrl())

// 渲染页面
/**
 *
 */
async function render(): Promise<void> {
  // microApp 初始化全局token
  await globalToken.init(getBaseApiUrl())
  // 判断用户信息
  // 触发初始化用户信息 pinia
  if (!(await useUserInfo().getUserInfo())) {
    // 获取登录信息失败, 跳转至错误页面
  }

  const router = await initRouter()
  const app = createApp(App)

  directive(app)

  app.use(pinia).use(router).use(CSUI, {
    elNamespace: 'qk'
  }).use(globalComponents).mount('#app')
}

render()
